package com.suxin.thread.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Tang
 * @classname WorkExecutor
 * @description [  ]
 * @date 2021/3/16 21:22
 */
@Slf4j
public class WorkExecutor {

    private static ThreadPoolExecutor WORKER;

    static {
        log.info("初始化工作线程池...");
        init();
        log.info("初始化工作线程池完成");
    }

    private static void init() {
        WORKER = new ThreadPoolExecutor(
                1,
                Runtime.getRuntime().availableProcessors() * 2,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(64),
                new CustomizableThreadFactory("scheduler-worker-pool-"),
                // 线程池放入失败，主线程直接执行
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

    public static void exec(RunnableTask task) {
        WORKER.execute(task);
    }

}